<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use think\facade\View;
use app\common\model\SystemMenu;
use app\common\model\LoginLog;
use app\common\model\Store;
use app\common\model\User;
use think\facade\Config;
use think\facade\Cache;


class Index extends Base
{
    public function Index()
    {
        View::assign('sysconfig', config('sysconfig'));
        return View::fetch();
    }

    // 初始化菜单
    public function init()
    {
        $init = [];
        $init['homeInfo'] = [
            "title" => "首页",
            "href" => "/index/welcome"
        ];
        $init['logoInfo'] = [
            "title" => config('sysconfig.admin_small_title'),
            "image" => "/system/" . Config::get('sysconfig')['admin_logo'],
            "href" => ""
        ];
        $systemModel = new SystemMenu();
        $menu = $systemModel->getMenu();
        $admin = cache('admin_user'.session('admin_user'));
        if($admin['is_supper'] == 1){
            $init['menuInfo'] = array_merge($menu, Config::get('sysmenu'));
        }else{
            $init['menuInfo'] = $menu;
        }
        
        return json($init);
    }
    public function welcome()
    {

        $LoginLog = new LoginLog();
        $Store = new Store();
        $User = new User();

        $year = date("Y");
        $month = date("m");
        $ri = date("d");

        $user_num =$User->count();

        $visit_d_num = $LoginLog->where(['YYY'=>$year,"MMM"=>$month,"DDD"=>$ri])->count();
        $visit_y_num = $LoginLog->where(['YYY'=>$year,"MMM"=>$month])->count();

        $store_num = $Store->count();


        View::assign("user_num",$user_num);
        View::assign("visit_d_num",$visit_d_num);
        View::assign("visit_y_num",$visit_y_num);
        View::assign("store_num",$store_num);
        
        return View::fetch();
    }
}
